package nku.csc364.updatedatabase;

import nku.csc364.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class SelectPictureActivity extends Activity {
	
	private Gallery gallery;
	private ImageView imgView;
	
	private Integer[] Imgid = {R.drawable.proj3_1, 
				R.drawable.proj3_2, 
				R.drawable.proj3_3, 
				R.drawable.proj3_4, 
				R.drawable.proj3_5, 
				R.drawable.proj3_6, 
				R.drawable.proj3_7,
				R.drawable.proj3_8,
				R.drawable.proj3_9,
				R.drawable.proj3_10,
				R.drawable.proj3_11,
				R.drawable.proj3_12,
				R.drawable.proj3_13,
				R.drawable.proj3_14};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectpicture);
        
		imgView = (ImageView)findViewById(R.id.ImageView01);
		imgView.setImageResource(Imgid[0]);
		
		gallery = (Gallery) findViewById(R.id.examplegallery);
		gallery.setAdapter(new AddImgAdp(this));

		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) 
			{
				imgView.setImageResource(Imgid[position]);
			}
		});
    }
    
    public void doSelect(View view){
    	int pictureNumber = gallery.getSelectedItemPosition();
    	System.out.println(pictureNumber);
		Intent i = getIntent();
		i.putExtra("pictureNumber", pictureNumber);
		setResult(RESULT_OK, i);
		finish();
    }
    
    
	public class AddImgAdp extends BaseAdapter 
	{
		int GalItemBg;
		private Context cont;

		public AddImgAdp(Context c) 
		{
			cont = c;
			TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
			GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
			typArray.recycle();
		}

		public int getCount() 
		{
			return Imgid.length;
		}

		public Object getItem(int position) 
		{
			return position;
		}

		public long getItemId(int position) 
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) 
		{
			ImageView imgView = new ImageView(cont);

			imgView.setImageResource(Imgid[position]);
			imgView.setLayoutParams(new Gallery.LayoutParams(100, 80));
			imgView.setScaleType(ImageView.ScaleType.FIT_XY);
			imgView.setBackgroundResource(GalItemBg);

			return imgView;
		}
	}

}